<template>
  <div>
    <div class="header">
      <el-page-header @back="goBack" :content="'学习路径节点管理 - ' + pathInfo.name"/>
    </div>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增节点</el-button>
          <el-button
              type="success"
              size="mini"
              icon="el-icon-check"
              :disabled="!hasSortChanged"
              @click="onSaveSort"
          >保存排序</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
          >
            <el-table-column type="index" label="序号" width="80"/>
            <el-table-column prop="node_name" label="知识点名称" min-width="200"/>
            <el-table-column prop="node_type" label="知识点类型" width="120">
              <template slot-scope="scope">
                {{ getNodeTypeName(scope.row.node_type) }}
              </template>
            </el-table-column>
            <el-table-column prop="requirements" label="学习要求" min-width="200"/>
            <el-table-column prop="estimated_time" label="预计学时" width="100">
              <template slot-scope="scope">
                {{ scope.row.estimated_time }}小时
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="150">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
    </div>
    <Add ref="addFormRef" @get-page="initData" :path-id="pathId"/>
  </div>
</template>

<script>
import { http } from "@/api";
import Add from "./node-add";

export default {
  name: "LearningPathNodes",
  components: {
    Add
  },
  data() {
    return {
      pathId: null,
      pathInfo: {},
      loading: false,
      tableData: [],
      originalData: []
    }
  },
  computed: {
    hasSortChanged() {
      if (this.originalData.length !== this.tableData.length) return true
      return this.tableData.some((item, index) => item.id !== this.originalData[index].id)
    }
  },
  created() {
    this.pathId = this.$route.query.id
    if (this.pathId) {
      this.getPathInfo()
      this.initData()
    }
  },
  methods: {
    getPathInfo() {
      http.learningPath.get(this.pathId).then(res => {
        this.pathInfo = res.data
      })
    },
    initData() {
      this.loading = true
      http.learningPath.getNodes(this.pathId).then(res => {
        this.tableData = res.data
        this.originalData = JSON.parse(JSON.stringify(res.data))
      }).finally(() => {
        this.loading = false
      })
    },
    getNodeTypeName(type) {
      const types = {
        1: '概念',
        2: '定理',
        3: '公式',
        4: '规则',
        5: '设备',
        6: '技术'
      }
      return types[type] || '-'
    },
    onAdd() {
      this.$refs.addFormRef.open()
    },
    onEdit(row) {
      this.$refs.addFormRef.open(row)
    },
    onDelete(row) {
      this.$confirm('确认删除该节点吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        http.learningPath.deleteNode(row.id).then(() => {
          this.$message.success('删除成功')
          this.initData()
        })
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    },
    onSaveSort() {
      const nodeIds = this.tableData.map(item => item.id)
      http.learningPath.updateNodeOrder(this.pathId, { node_ids: nodeIds }).then(() => {
        this.$message.success('保存成功')
        this.initData()
      })
    },
    goBack() {
      this.$router.push('/knowledge/path')
    }
  }
}
</script>

<style scoped lang="scss">
.header {
  margin-bottom: 20px;
}
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
  .el-button {
    margin-right: 10px;
  }
}
</style> 